package com.ts.sms.sendUtils;

import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
import com.ts.sms.dto.Sms;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SendSms {
    public static void send(Sms sms) {
        String path = "https://dfsms.api.bdymkt.com/send_sms";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
        request.setCredentials("0f1c6d40502a4e62bc29c8e5c15509b0", "ca2ac6ac4e294e208d19f8f40e77c09c");
        // 设置header参数
        request.addHeaderParameter("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        // 设置jsonBody参数
        String jsonBody = "phone_number="+sms.getPhone()+"&template_id=TPL_0001&content=code:"+sms.getContent()+",expire_at:5";
        request.setJsonBody(jsonBody);
        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());
        try {
          ApiExplorerResponse response = client.sendRequest(request);
          // 返回结果格式为Json字符串
            log.debug(response.getResult());
        } catch (Exception e) {
          e.printStackTrace();
        }
    }
}